package jxc.servlet.info.goods;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxc.data.hibernate.dao.GoodsDAO;
import jxc.data.hibernate.pojo.Goods;

/**
 * Servlet implementation class List
 */
@WebServlet("/info/goods/list")
public class List extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	/**
	 * 列出商品
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		GoodsDAO dao=new GoodsDAO();
		java.util.List<Goods> goods=dao.getAll();
		request.setAttribute("goods", goods);
		request.getRequestDispatcher("/info/goods/list.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name=request.getParameter("name");
		String gid=request.getParameter("id");
		GoodsDAO dao=new GoodsDAO();
		java.util.List<Goods> goods=null;
		if(gid!=null&&gid.length()>0){
			//根据商品编号查找
			goods=new ArrayList<Goods>();
			Goods good=dao.getById(gid);
			if(good!=null)
				goods.add(good);
		}else if(name!=null&&name.length()>0){
			goods=dao.getLikeName(name);
		}else
			goods=dao.getAll();
		request.setAttribute("goods", goods);
		request.getRequestDispatcher("/info/goods/list.jsp").forward(request, response);
	}

}
